<?php

class Admin_RatesController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        $this->_helper->layout()->disableLayout();
        Zend_Loader::loadFile('../application/admin/models/Rates.php');
        $hotel_id = $this->_getParam('hotel_id', 0);
        $rates_model = new Rates();
        $rate_arr = $rates_model->listRates($hotel_id);
        // print_r($newsletter_arr); EXIT;
        $this->view->hotel_id = $hotel_id;
        $this->view->rate_arr = $rate_arr;
     
    }

    public function addAction()
    {
        $this->_helper->layout()->disableLayout();
		Zend_Loader::loadFile('../application/admin/forms/Rates.php');
      
        $this->view->title = "Add New Rate";
        $hotel_id = $this->_getParam('hotel_id', 0);
        $options = array("hotel_id"=>$hotel_id);
        $rates_form = new Form_Rates($options);
        $this->view->hotel_id = $hotel_id;
        $this->view->rates_form = $rates_form;            
    }
    
    public function doaddratesAction()
    {
        
    	Zend_Loader::loadFile('../application/admin/forms/Rates.php');
		Zend_Loader::loadFile('../application/admin/models/Rates.php');
		$form_rates = new Form_Rates();
		$request = $this->getRequest();
		
		if ($this->getRequest()->isPost()) {            
			$rates_model = new Rates();
			$form_values = $request->getPost();
			$add_rates = $rates_model->addRate($form_values); 
		}
		
		exit;
    }

    public function editAction()
    {
        $this->_helper->layout()->disableLayout();
		Zend_Loader::loadFile('../application/admin/forms/Rates.php');
        Zend_Loader::loadFile('../application/admin/models/Rates.php');
        $rate_model = new Rates();
        $controller = $this->getRequest()->getControllerName();
		$action = $this->getRequest()->getActionName();
        $this->view->title = "Add new Rate";
        $hotel_id = $this->_getParam('hotel_id', 0);
        $rate_id = $this->_getParam('rate_id', 0);
        $options = array("action"=>$action, "rate_id"=>$rate_id);
        $rate_form = new Form_Rates($options);
        $this->view->rate_form = $rate_form;
        $this->view->rate_id = $rate_id;
        if($rate_id > 0)
        {
            $rate_form->populate($rate_model->getrate($rate_id));                
        }
    }

   	public function updateratesAction()
   	{
   		$this->_helper->layout()->disableLayout();
   		Zend_Loader::loadFile('../application/admin/models/Rates.php');
		Zend_Loader::loadFile('../application/admin/forms/Rates.php');
        $rate_form = new Form_Rates();
        $request = $this->getRequest();
        if ($this->getRequest()->isPost()) {            
			$rate_model = new Rates();
			$form_values = $request->getPost();
			$update_rates = $rate_model->updateRate($form_values);
		}
		
		exit;
   	}
    
	public function deleteAction()
    {
        $this->_helper->layout()->disableLayout();
        $rate_id = $this->_getParam('rate_id', 0);    
        Zend_Loader::loadFile('../application/admin/models/Rates.php');
        $rate_model = new Rates();
        $rate_model->deleteRate($rate_id);
        //$this->_helper->redirector('index');
        exit;
    }

    public function ratefeaturesAction()
    {
        $this->_helper->layout()->disableLayout();
        Zend_Loader::loadFile('../application/admin/forms/Rates.php');
        Zend_Loader::loadFile('../application/admin/models/Rates.php');
        $rate_model = new Rates();
        $controller = $this->getRequest()->getControllerName();
        $action = $this->getRequest()->getActionName();
        $this->view->title = "Add new Rate";
        $hotel_id = $this->_getParam('hotel_id', 0);
        $rate_id = $this->_getParam('rate_id', 0);
        $options = array("action"=>$action, "rate_id"=>$rate_id);
        $structarr=$rate_model->getratefeatures($rate_id);
     // print_r($structarr);
        $this->view->structarr = $structarr;
        $this->view->rate_id = $rate_id;
        $this->view->hotel_id = $hotel_id;
        
    }

     public function doaddratefeaturesAction()
    {
         $this->_helper->layout()->disableLayout();
        Zend_Loader::loadFile('../application/admin/forms/Rates.php');
        Zend_Loader::loadFile('../application/admin/models/Rates.php');
        $rate_model = new Rates();
        $controller = $this->getRequest()->getControllerName();
        $action = $this->getRequest()->getActionName();
         if ($this->getRequest()->isPost()) {
			$rate_model = new Rates();
			$form_values = $this->getRequest()->getPost();
                $elementarray=array();
                $ratekeyval=array();
                $ratekey=array();
                $ratetype=array();
                $i=0;
                foreach($form_values as $x){
                  foreach($x as $y){
                      switch($y["name"]){
                          case "ratekey":
                              $ratekey[]=$y["value"];
                               break;
                          case "ratekeyvalFB":
                              $ratekeyvalFB[]=$y["value"];
                               break;
                          case "ratekeyvalHB":
                              $ratekeyvalHB[]=$y["value"];
                               break;
                          case "rate_id":
                              $elementarray["rate_id"]=$y["value"];
                           case "hotel_id":
                              $elementarray["hotel_id"]=$y["value"];

                      }
                  }
                }

                foreach($ratekey as $structkeyoptkey=>$structkeyoptval){
                    $elementarray["ratekey"][$structkeyoptkey]=$ratekey[$structkeyoptkey];
                    $elementarray["ratekeyvalFB"][$structkeyoptkey]=$ratekeyvalFB[$structkeyoptkey];
                    $elementarray["ratekeyvalHB"][$structkeyoptkey]=$ratekeyvalHB[$structkeyoptkey];

                }

               // print_r($elementarray);
		$update_rates = $rate_model->updateratefeatures($elementarray);
          }



    }

    public function deletestructuredataAction()
    {
        $this->_helper->layout()->disableLayout();
        Zend_Loader::loadFile('../application/admin/forms/Rates.php');
        Zend_Loader::loadFile('../application/admin/models/Rates.php');
        $rate_model = new Rates();
        $controller = $this->getRequest()->getControllerName();
        $action = $this->getRequest()->getActionName();
         if ($this->getRequest()->isPost()) {
             $form_values = $this->getRequest()->getPost();
             $deletestrdata = $rate_model->deleteratefeatures($form_values);
         }
         
    }
}